//
//  CWHomeTableViewCell.swift
//  SinaBlog
//
//  Created by ChenWei on 15/11/18.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

enum CellReusabeID: String {
    /// 首发cell
    case normalCell = "normalCell"
    /// 转发cell
    case forwardCell = "forwardCell"
    
    static func getReusableCellID(status: Status) -> String {
        return status.retweeted_status == nil ? CellReusabeID.normalCell.rawValue : CellReusabeID.forwardCell.rawValue
    }
}

class CWHomeTableViewCell: UITableViewCell {
    /// 微博模型
    var status: Status? {
        didSet{
            // 1.设置topView数据
            topView.status = status
            
            // 2.设置正文数据
            contentLabel.text = status?.text
            
            // 3.设置pictureView数据
            pictureView.status = status
            
            // 4.设置footView数据
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 1.初始化UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 内部方法
    func setupUI() {
        // 1.添加子控
        // 1.1添加topView
        contentView.addSubview(topView)
        
        // 1.2添加contentLabel
        contentView.addSubview(contentLabel)
        
        // 1.3添加pictureView
        contentView.addSubview(pictureView)
        
        // 1.4添加footview
        contentView.addSubview(footView)
        
        // 2.布局子控件
        // 2.1布局topView
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
        }
        
        // 2.2布局正文
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(topView).offset(10)
            make.top.equalTo(topView.snp_bottom).offset(10)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 20)
        }
        
        // 2.4布局底部视图
        footView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(10)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
            make.height.equalTo(44)
            
            // 由于已经根据cell数据算出cell的高度了,此约束就多余了
            // make.bottom.equalTo(contentView)
        }
    }
    
    // MARK: - 计算cell的行高
    func rowHeight(status: Status)-> CGFloat {
        // 1.给微博模型赋值数据(只有赋值后才能算出行高)
        self.status = status
        
        // 2.强制更新
        layoutIfNeeded()
        
        // 3.获得行高返回
        return CGRectGetMaxY(footView.frame)
    }

    // MARK: - 懒加载
    /// 顶部
    private lazy var topView: CWHomeCellTopView = {
        let topView = CWHomeCellTopView()

        return topView
    }()
        
    /// 正文
    lazy var contentLabel: UILabel = {
        let lb = UILabel()
        lb.numberOfLines = 0
        lb.text = "冲击dfdsfsdfsf20K"
        return lb
        }()
    
    /// 配图
    lazy var pictureView: CWHomePictureCollectionView = CWHomePictureCollectionView()
    
    /// 底部视图
    private lazy var footView: CWHomeTableVewCellFootView = {
        let footV = CWHomeTableVewCellFootView()
//        footV.backgroundColor = UIColor.lightGrayColor()
        
        return footV
    }()
}











